const mydiv = document.getElementById("bar");
// 需要补充一个开关
mydiv.onmousedown = function (event) {
  document.onmousemove = function () {
    // x 和 y 的最大值和最小值略
    let x2 = window.event.clientX,y2 = window.event.clientY;
    mydiv.style.top = `${y2}px`
    mydiv.style.left = `${x2}px`
  }
}
// 拖拽行为一定要记得解绑mousemove事件
window.onmouseup = function () {
  document.onmousemove = null
}

